package org.osflash.signals;

import asunit.asserts.*;
import asunit.framework.IAsync;

class PriorityListenersTest {

	@:meta(Inject())
	public var async : IAsync;
	public var completed : DeluxeSignal;
	var listenersCalled : Array<Dynamic>;
	@:meta(Before())
	public function setUp() : Void {
		completed = new DeluxeSignal(this);
		listenersCalled = [];
	}

	@:meta(After())
	public function tearDown() : Void {
		completed.removeAll();
		completed = null;
		listenersCalled = null;
	}

	//////
	@:meta(Test())
	public function listener_added_second_with_higher_priority_should_be_called_first() : Void {
		completed.addWithPriority(async.add(listener1, 5));
		completed.addWithPriority(async.add(listener0, 5), 10);
		completed.dispatch();
	}

	function listener0() : Void {
		listenersCalled.push(arguments.callee);
		assertSame("this should be the first listener called", arguments.callee, listenersCalled[0]);
	}

	function listener1() : Void {
		listenersCalled.push(arguments.callee);
		assertSame("this should be the second listener called", arguments.callee, listenersCalled[1]);
	}

	function listener2() : Void {
		listenersCalled.push(arguments.callee);
		assertSame("this should be the third listener called", arguments.callee, listenersCalled[2]);
	}

	//////
	@:meta(Test())
	public function listeners_added_with_same_priority_should_be_called_in_order_added() : Void {
		completed.addWithPriority(async.add(listener0, 5), 10);
		completed.addWithPriority(async.add(listener1, 5), 10);
		completed.addWithPriority(async.add(listener2, 5), 10);
		completed.dispatch();
	}

}

